//
//  RegisterViewController.m
//  YiChat20160706
//
//  Created by 李政 on 16/7/6.
//  Copyright © 2016年 ZhiYou. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (IBAction)confirmRegisterButton:(id)sender
{
    if (![self checkIsNull])
    {
        return;
    }
    
    AFHTTPSessionManager *mgr = [NetTool getSessionManager];
    
    if (mgr)
    {
        NSDictionary *parameterDic = @{
                                       @"command":@"ST_R",
                                       @"name":_userNameTF.text,
                                       @"psw":_pswTF.text,
                                       @"nickname":_nickNameTF.text,
                                       @"email":_emailTF.text
                                       };
        
        [mgr POST:[self getNormalInterfaceUrl] parameters:parameterDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            [NetTool hideIndicator];
            
            NSLog(@"------%@",[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
            
            if (responseObject)
            {
               NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                if ([resultDic[@"result"] isEqualToString:@"1"])
                {
                    [Util showAlertWithMessage:REGISTER_SUCCESS confirmBlock:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }
                else
                {
                    [Util showAlertWithMessage:USER_HAD_REGISTER confirmBlock:^
                    {
                        [_userNameTF becomeFirstResponder];
                    }];
                }
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [NetTool hideIndicator];
            [Util showAlertWithMessage:LOAD_FAIL confirmBlock:nil];
        }];

    }
}


- (BOOL)checkIsNull
{
    
    
    if ([_userNameTF.text isEqualToString:@""])
    {
        
        
        [Util showAlertWithMessage:USER_NAME_NOTNULL confirmBlock:^
        {
            [_userNameTF becomeFirstResponder];
        }];
        
        
        return NO;
    }
    
    if ([_pswTF.text isEqualToString:@""])
    {
        [Util showAlertWithMessage:PSW_NOTNULL confirmBlock:^
        {
            [_pswTF becomeFirstResponder];
        }];
        return NO;
    }
    
    if (![_pswTF.text isEqualToString:_confirmPswTF.text])
    {
        [Util showAlertWithMessage:PSW_NOT_SAME confirmBlock:^
        {
            [_confirmPswTF becomeFirstResponder];
        }];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
